package POJO;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


@Entity
@Table(name = "PRODUCT_DETAIL", uniqueConstraints = {
		@UniqueConstraint(columnNames = "Product_Detail_Id"),
		@UniqueConstraint(columnNames = "Price")
})
public class ProductDetail implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -863607284975345471L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "Product_Detail_Id", unique = true, nullable = false)
	private int productDetailId;
	
	@Column(name = "Count")
	private int count;
	
	@Column(name = "Price", unique = true, nullable = false)
	private double price;
	
	@Column(name = "Old_Price")
	private double oldPrice;
	
	@Column(name = "Deleted")
	private int deleted;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "Product_Id", nullable = false)
	private Product product;

	@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
	@JoinTable(name = "PRODUCT_DETAIL_TYPE", joinColumns = {
			@JoinColumn(name = "Product_Detail_Id", nullable = false, updatable = false)},
			inverseJoinColumns = { @JoinColumn(name = "Type_Detail_Id", 
												nullable = false, updatable = false)})
	private Set<TypeDetail> typeDetails = new HashSet<TypeDetail>();

	
	@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
	@JoinTable(name = "PRODUCT_DETAIL_IMAGE", joinColumns = {
			@JoinColumn(name = "Product_Detail_Id", nullable = false, updatable = false)},
			inverseJoinColumns = { @JoinColumn(name = "Image_Id", 
												nullable = false, updatable = false)})
	private Set<Image> images = new HashSet<Image>();
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.productDetail")
	private Set<StoreProductDetail> storeProductDetails = new HashSet<StoreProductDetail>();

	@ManyToOne()
	@JoinColumn(name = "Image_Id")
	private Image image;

	public ProductDetail() {
		super();
	}

	public ProductDetail(int productDetailId, int count, double price,
			double oldPrice, int deleted, Product product) {
		super();
		this.productDetailId = productDetailId;
		this.count = count;
		this.price = price;
		this.oldPrice = oldPrice;
		this.deleted = deleted;
		this.product = product;
	}

	public ProductDetail(int count, double price, double oldPrice, int deleted,
			Product product, Set<TypeDetail> typeDetails) {
		super();
		this.count = count;
		this.price = price;
		this.oldPrice = oldPrice;
		this.deleted = deleted;
		this.product = product;
		this.typeDetails = typeDetails;
	}

	public ProductDetail(int count, double price, double oldPrice, int deleted,
			Product product, Set<TypeDetail> typeDetails, Set<Image> images, Image image) {
		super();
		this.count = count;
		this.price = price;
		this.oldPrice = oldPrice;
		this.deleted = deleted;
		this.product = product;
		this.typeDetails = typeDetails;
		this.images = images;
		this.image = image;
	}

	public int getProductDetailId() {
		return productDetailId;
	}

	public void setProductDetailId(int productDetailId) {
		this.productDetailId = productDetailId;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getOldPrice() {
		return oldPrice;
	}

	public void setOldPrice(double oldPrice) {
		this.oldPrice = oldPrice;
	}

	public int getDeleted() {
		return deleted;
	}

	public void setDeleted(int deleted) {
		this.deleted = deleted;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
	
	public Set<TypeDetail> getTypeDetails() {
		return typeDetails;
	}

	public void setTypeDetails(Set<TypeDetail> typeDetails) {
		this.typeDetails = typeDetails;
	}
	
	public Set<Image> getImages() {
		return images;
	}

	public void setImages(Set<Image> images) {
		this.images = images;
	}
	
	public Set<StoreProductDetail> getStoreProductDetails() {
		return storeProductDetails;
	}

	public void setStoreProductDetails(Set<StoreProductDetail> storeProductDetails) {
		this.storeProductDetails = storeProductDetails;
	}
	
	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}
	
	
	public String getName()
	{
		String type = "";
		for(TypeDetail detail : typeDetails)
		{
			type += detail.getType().getTypeName() + ": " + detail.getTypeDetailName() + " ";
		}
		return product.getProductName() +"  "+ type;		
	}
}











